<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

handleAutoTrade( 'gold', true );
handleAutoTrade( 'gold', false );
handleAutoTrade( 'iron', true );
handleAutoTrade( 'iron', false );
handleAutoTrade( 'coal', true );
handleAutoTrade( 'coal', false );
handleAutoTrade( 'food', true );
handleAutoTrade( 'food', false );

header( 'Location: manage_auto_trade.php' );

function handleAutoTrade( $for, $below ) {
	global $dal;
	
	$prefix = $for . '_' . ( $below ? 'below' : 'above' ) . '_';
	
	$enabled = getPostValue( $prefix . 'enable' );
	$amount = getPostValue( $prefix . 'amount' );
	$against = getPostValue( $prefix . 'against' );
	$pricing = getPostValue( $prefix . 'pricing' );
	
	if( $below ) {
		$belowValueSetting = 1;
		$belowValue = getPostValue( $prefix . 'below' );
		$aboveValue = 0;
		$resourceOffered = $against;
		$resourceRequested = $for;
		$statement = $dal->prepare( 'SELECT * ' .
				'FROM auto_trade ' .
				'WHERE ruler_id = :ruler_id AND resource_requested = :resource_requested AND below = :below' );
		$statement->bindParam( ':resource_requested', $resourceRequested, PDO::PARAM_STR );
	}
	else {
		$belowValueSetting = 0;
		$aboveValue = getPostValue( $prefix . 'above' );
		$belowValue = 0;
		$resourceOffered = $for;
		$resourceRequested = $against;
		$statement = $dal->prepare( 'SELECT * ' .
				'FROM auto_trade ' .
				'WHERE ruler_id = :ruler_id AND resource_offered = :resource_offered AND below = :below' );
		$statement->bindParam( ':resource_offered', $resourceOffered, PDO::PARAM_STR );
	}
	$statement->bindParam( ':below', $belowValueSetting, PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$exists = $dal->getValue( $statement );

	if( $exists > 0 ) {
		if( $below ) {
			$statement = $dal->prepare( 'UPDATE auto_trade ' .
					'SET `enabled` = :enabled, `resource_offered` = :resource_offered, `amount` = :amount, `pricing`= :pricing, ' .
						'`trade_over` = :trade_over, `trade_below` = :trade_below ' .
					'WHERE `ruler_id` = :ruler_id AND `resource_requested` = :resource_requested AND `below` = :below' );
		}
		else {
			$statement = $dal->prepare( 'UPDATE auto_trade ' .
					'SET `enabled` = :enabled, `amount` = :amount, `pricing`= :pricing, ' .
						'`resource_requested` = :resource_requested, `trade_over` = :trade_over, `trade_below` = :trade_below ' .
					'WHERE `ruler_id` = :ruler_id AND `resource_offered` = :resource_offered AND `below` = :below' );
		}
	}
	else {
		$statement = $dal->prepare( 'INSERT INTO auto_trade ' .
				'( `ruler_id`, `enabled`, `resource_offered`, `resource_requested`, `amount`, ' .
					'`trade_over`, `trade_below`, `pricing`, `below` ) ' .
				'VALUES( :ruler_id, :enabled, :resource_offered, :resource_requested, :amount, ' .
					':trade_over, :trade_below, :pricing, :below )' );
	}
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$statement->bindParam( ':enabled', $enabled, PDO::PARAM_INT );
	$statement->bindParam( ':resource_offered', $resourceOffered, PDO::PARAM_STR );
	$statement->bindParam( ':resource_requested', $resourceRequested, PDO::PARAM_STR );
	$statement->bindParam( ':amount', $amount, PDO::PARAM_INT );
	$statement->bindParam( ':trade_over', $aboveValue, PDO::PARAM_INT );
	$statement->bindParam( ':trade_below', $belowValue, PDO::PARAM_INT );
	$statement->bindParam( ':pricing', $pricing, PDO::PARAM_INT );
	$statement->bindParam( ':below', $belowValueSetting, PDO::PARAM_INT );
	$dal->execute( $statement );
}
?>
